package com.bazaaroid.mobile.android.adapter;

import java.util.ArrayList;

import com.bazaaroid.mobile.android.R;
import com.bazaaroid.mobile.db.model.partner.Product;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class ProductsAdapter extends ArrayAdapter<Product> implements AdapterView.OnItemClickListener
{
    private ArrayList<Product> items;

    public ProductsAdapter(Context context, int textViewResourceId, ArrayList<Product> items) 
    {
            super(context, textViewResourceId, items);
            this.items = items;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
    	View v = convertView;
    	if(v == null)
    	{
            LayoutInflater vi = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.products_row, null);
            
    	}
    	Product product = items.get(position);
        if (product != null) 
        {
                TextView tt = (TextView) v.findViewById(R.id.toptext);
                TextView bt = (TextView) v.findViewById(R.id.bottomtext);
                TextView prodPrice = (TextView) v.findViewById(R.id.price);
                TextView prodCurrency = (TextView) v.findViewById(R.id.currency);
                if(tt != null)
                    tt.setText(product.getName());
                if(bt != null)
                    bt.setText(product.getDescription());
                if(prodPrice != null)
                	prodPrice.setText(String.format("%d", (int)(product.getPrice())));
                if(prodCurrency != null)
                	prodCurrency.setText(product.getCurrency());
                
        }
		return v;
	}
    
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
    {
    	Dialog dialog = new Dialog(getContext());
    	dialog.setTitle(String.format("%d/%d", position, items.size()));
    }
}
